package com.m2bshop.shop.controller;


import com.m2bshop.shop.pojo.Wallet;
import com.m2bshop.shop.service.WalletService.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.support.RequestPartServletServerHttpRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;

@RequestMapping("/wallet")
@RestController
public class WalletController {
    @Autowired
    WalletService walletService;

    @RequestMapping("/WalletInfo/{userid}")
    public Wallet getWalletInfo(@PathVariable int userid){
        return walletService.getWalletInfo(userid);
    }
    @RequestMapping("/GetBan/{userid}")
    public double getBan(@PathVariable int userid)
    {
        return walletService.getWalletInfo(userid).getBalance();
    }
    @RequestMapping("/pass/{password}/{userid}")
    public String  pass(@PathVariable String password,@PathVariable int userid){
        String info=null;
        if(password.equals(walletService.getWalletInfo(userid).getPayment()))
        {
            info="密码正确";
        }
        else{
            info="密码错误";
        }
        return info;
    }
    @RequestMapping("/Insert")//新建钱包
    public String InsertNewWallet(@RequestBody Wallet wallet){
        return walletService.InsertNewWallet(wallet);
    }
    @RequestMapping("/Recharge/{money}/{userid}/{bankid}")//充值
    public String Recharge(@PathVariable double money,@PathVariable int userid,@PathVariable int bankid){
        return walletService.UpdateBalanceByBank(walletService.getWalletInfo(userid),0,money,bankid);
    }
    @RequestMapping("/change/{money}/{userid}/{bankid}")//提现
    public String change(@PathVariable double money,@PathVariable int userid,@PathVariable int bankid){
        return walletService.UpdateBalanceByBank(walletService.getWalletInfo(userid),1,money,bankid);
    }
    @RequestMapping("/expense/{userid}/{money}")//支付
    public String expense(@PathVariable int userid,@PathVariable double money){
        return walletService.UpdateBalanceByExpense(userid,money);
    }
    @RequestMapping("/UpdateInfo")
    public String Update(@RequestBody Wallet wallet){
        return walletService.UpdateWallet(wallet);
    }
    @RequestMapping("/getIsWallet/{userid}")//新建钱包判断
    public int get(@PathVariable int userid){
        return walletService.getUserWallet(userid);
    }

}
